package com.haoshui.utils;

import com.haoshui.domain.entity.Article;
import com.haoshui.domain.vo.HotArticleVo;
import org.springframework.beans.BeanUtils;

import java.util.List;
import java.util.stream.Collectors;


public class BeanCopyUitls {

    private BeanCopyUitls(){

    }

    public static <V> V copyBean(Object source, Class<V> clazz){
        //创建对象
        V result = null;
        try {
             result =  clazz.newInstance();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }

        //复制属性
        BeanUtils.copyProperties(source, result);

        return result;
    }

    public static <O, V> List<V> copyBeanList(List<O> source,  Class<V> clazz){
        return  source.stream()
                .map(O->copyBean(O, clazz))
                .collect(Collectors.toList());

    }

    public static void main(String[] args) {
        copyBean(new Article(), HotArticleVo.class);
    }
}
